package com.hex.ds.hdtp.core.web.data.contrast.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastTemplateRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastTemplateService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.converter.ICrossDbContrastTemplateVoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.vo.CrossDbContrastTemplateVo;
import com.hex.ds.hdtp.core.web.data.contrast.vo.query.CrossDbContrastTemplateQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.contrast.controller
 * @ClassName CrossDbContrastTemplateController
 * @Description: 跨库对比模板-WEB接口
 * @Author: wz.li
 * @Date 2023/7/24 19:13
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "05-跨库比对：模板配置")
@RequestMapping("/api/cross/db/contrast/template")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastTemplateController {

    private final ICrossDbContrastTemplateService crossDbContrastTemplateService;

    private final ICrossDbContrastTemplateVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastTemplateQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比模板-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:16 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("跨库对比模板-分页查询")
    public ResponseEntity queryListByPage(CrossDbContrastTemplateQueryVo crossDbContrastTemplateQueryVo, PageVo pageVo) {
        log.debug("进入到【CrossDbContrastTemplateController.queryListByPage】方法,参数：{}" + crossDbContrastTemplateQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        CrossDbContrastTemplateRequestDto requestDto = converter.toRequestDtoByQuery(crossDbContrastTemplateQueryVo);
        return ResponseEntity.ok(crossDbContrastTemplateService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method: add <br>
     * @Param: [crossDbContrastTemplateVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比模板-新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:18 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比模板-新增")
    @PostMapping
    @ApiOperation("跨库对比模板-新增")
    public ResponseEntity add(@RequestBody CrossDbContrastTemplateVo crossDbContrastTemplateVo) {
        log.info("进入到【CrossDbContrastTemplateController.add】方法,参数：{}", crossDbContrastTemplateVo.toString());
        crossDbContrastTemplateService.add(converter.toRequestDto(crossDbContrastTemplateVo));
        return ResponseEntity.ok("新增成功");
    }

    /*
     * @Method: update <br>
     * @Param: [crossDbContrastTemplateVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比模板-更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:18 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比模板-更新")
    @PutMapping
    @ApiOperation("跨库对比模板-更新")
    public ResponseEntity update(@RequestBody CrossDbContrastTemplateVo crossDbContrastTemplateVo) {
        log.info("进入到【CrossDbContrastTemplateController.update】方法,参数：{}", crossDbContrastTemplateVo.toString());
        crossDbContrastTemplateService.modifyById(converter.toRequestDto(crossDbContrastTemplateVo));
        return ResponseEntity.ok("更新成功");
    }

    /*
     * @Method: delete <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比模板-批量删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:18 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库对比模板-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("跨库对比模板-批量删除")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【CrossDbContrastTemplateController.delete】方法,参数：" + pkIds);
        crossDbContrastTemplateService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    @Log("跨库对比模板-导出")
    @GetMapping("/exportData")
    @ApiOperation("跨库对比模板-导出")
    public ResponseEntity exportData(@RequestParam @NotNull Map<String, String> params) throws IOException {
        log.info("导出比对模板【CrossDbContrastTemplateController.exportData】");
        List<String> pkIds = new ArrayList<>(params.values());
        String filePath = crossDbContrastTemplateService.exportData(pkIds);
        byte[] bytes = Files.readAllBytes(new File(filePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=contrastTemplate.zip")
                .body(bar);
    }

    @Log("跨库对比模板-导出")
    @PostMapping("/upload")
    @ApiOperation("跨库对比模板-导入")
    public ResponseEntity<Object> upload(HttpServletRequest request) {
        log.info("【CrossDbContrastTemplateController.upload】导入");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map resultMap = crossDbContrastTemplateService.importExcel(multipartRequest.getFile("file"));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }
}
